package com.vito.oa.act.extend.action;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vito.act.action.ActExTableAction;
import com.vito.act.model.ActExTableModel;
import com.vito.act.service.ActExTableService;
import com.vito.base.authoriza.action.BaseAction;
import com.vito.base.authoriza.cache.SessionMap;
import com.vito.base.authoriza.model.UserModel;
import com.vito.base.model.ResMsg;
import com.vito.base.model.Result;
import com.vito.base.util.Contants;
import com.vito.base.util.Validata;
import com.vito.oa.act.extend.model.OaActExTableModel;
@Controller
@RequestMapping("oa/act/actExTable")
public class OaActExTableAction extends BaseAction{
	private static final Logger log = LoggerFactory.getLogger(ActExTableAction.class);
	@Reference(group="OaActExTableServiceImpl")
	private ActExTableService actExTableService;
	
	@RequestMapping("queryActExTableByPage")
	@ResponseBody
	public Result queryActExTableByPage(ActExTableModel actExTableModel){
		Result rs = new Result();
		try{
			//获取当前登录用户
			UserModel user = (UserModel) SessionMap.getObj();
			if(user == null || Validata.isNullOrEmpty(user.getUserId())){
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}
			actExTableModel.setCreateDept(user.getDeptParentId());
			actExTableModel.setFirstName(Contants.TABLE_FIRST_NAME.toUpperCase());
			rs.setData(actExTableService.queryActExTableByPage(actExTableModel));
			rs.setCode(Result.CODE_OK);
		}catch(Exception e){
			log.error("查询异常：",e);
			rs.setCode(Result.CODE_ERROR);
		}
		return rs;
	}
	@RequestMapping("updateActExTable")
	@ResponseBody
	public Result updateActExTable(ActExTableModel actExTableModel){
		Result rs = new Result();
		try{
			int count = actExTableService.updateActExTable(actExTableModel);
			if(count>0){
				rs.setCode(Result.CODE_OK);
				rs.setMsg(ResMsg.UPDSUC);
			}else{
				rs.setCode(Result.CODE_OK);
				rs.setMsg(ResMsg.UPDERR);
			}
		}catch(Exception e){
			log.error("更新异常：",e);
			rs.setCode(Result.CODE_ERROR);
		}
		return rs;
	}
	@RequestMapping("saveActExTable")
	@ResponseBody
	public Result saveActExTable(OaActExTableModel actExTableModel){
		Result rs = new Result();
		try{
			int count = actExTableService.saveActExTable(actExTableModel);
			if(count>0){
				rs.setCode(Result.CODE_OK);
				rs.setMsg(ResMsg.SAVESUC);
			}else{
				rs.setCode(Result.CODE_OK);
				rs.setMsg(ResMsg.SAVEERR);
			}
		}catch(Exception e){
			log.error("保存异常：",e);
			rs.setCode(Result.CODE_ERROR);
		}
		return rs;
	}
	@RequestMapping("deleteActExTable")
	@ResponseBody
	public Result deleteActExTable(ActExTableModel actExTableModel){
		Result rs = new Result();
		try{
			int count = actExTableService.deleteActExTable(actExTableModel);
			if(count>0){
				rs.setCode(Result.CODE_OK);
				rs.setMsg(ResMsg.DELSUC);
			}else{
				rs.setCode(Result.CODE_OK);
				rs.setMsg(ResMsg.DELERR);
			}
		}catch(Exception e){
			log.error("删除异常：",e);
			rs.setCode(Result.CODE_ERROR);
		}
		return rs;
	}
	@RequestMapping("queryActExTableTree")
	@ResponseBody
	public Result queryActExTableTree(ActExTableModel actExTableModel){
		Result rs = new Result();
		try{
			rs.setData(actExTableService.queryActExTableTree(actExTableModel));
			rs.setCode(Result.CODE_OK);
		}catch(Exception e){
			log.error("查询异常：",e);
			rs.setCode(Result.CODE_ERROR);
		}
		return rs;
	}
	@RequestMapping("queryActExTableList")
	@ResponseBody
	public Result queryActExTableList(ActExTableModel actExTableModel){
		Result rs = new Result();
		try{
			rs.setData(actExTableService.queryTableList(actExTableModel));
			rs.setCode(Result.CODE_OK);
		}catch(Exception e){
			log.error("查询异常：",e);
			rs.setCode(Result.CODE_ERROR);
		}
		return rs;
	}
	@RequestMapping("queryTableListByBuss")
	@ResponseBody
	public Result queryTableListByBuss(String businessId,String tableKey){
		Result rs = new Result();
		try{
			Map<String, String> map = new HashMap<String, String>();
			map.put("businessId", businessId);
			map.put("tableKey", tableKey);
			rs.setData(actExTableService.queryTableListByBuss(map));
			rs.setCode(Result.CODE_OK);
		}catch(Exception e){
			log.error("查询异常：",e);
			rs.setCode(Result.CODE_ERROR);
		}
		return rs;
	}
	@RequestMapping("addTableToDbByTableKey")
	@ResponseBody
	public Result addTableToDbByTableKey(String tableKey){
		Result rs = new Result();
		try{
			return actExTableService.addTableToDbByTableKey(tableKey);
		}catch(Exception e){
			log.error("创建表异常：",e);
			rs.setCode(Result.CODE_ERROR);
		}
		return rs;
	}
}